home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / forms / FORMS / choice.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  232 lines

  1. /*
  2.  * choice.c
  3.  *
  4.  * Forms Object class: CHOICE
  5.  *
  6.  * Written by: Mark Overmars and Trevor Paquette
  7.  *
  8.  * Version 2.1 b
  9.  * Date: Oct  1, 1992
  10.  */
  11.  
  12. #include <malloc.h>
  13. #include <strings.h>
  14. #include "gl/device.h"
  15. #include "gl/gl.h"
  16. #include "forms.h"
  17.  
  18. /* Object specific information */
  19. typedef struct{
  20.    int  val;                    /* last menu item selected */
  21.    int  numitems;               /* number of items in choice */
  22.    float fontsize;              /* font size */
  23.    int fontstyle;               /* font style */
  24.    char *items[FL_CHOICE_MAXITEMS+1]; /* individual choice items */
  25.    int counter;            /* counts mouse events */
  26. } SPEC;
  27.  
  28. static void draw_choice(FL_OBJECT *ob)
  29. /* Draws a choice object */
  30. {
  31.   int c1;
  32.   SPEC *sp = ((SPEC *)(ob->spec));
  33.   if(ob->belowmouse && ob->col1 == FL_CHOICE_COL1)
  34.      c1 = FL_CHOICE_MCOL;
  35.   else
  36.     c1 = ob->col1;
  37.   fl_drw_box(ob->boxtype,ob->x,ob->y,ob->w,ob->h,c1,FL_CHOICE_BW);
  38.   fl_drw_text_beside(ob->align,ob->x,ob->y,ob->w,ob->h,ob->lcol,
  39.                         ob->lsize,ob->lstyle,ob->label);
  40.   if (sp->val >0)
  41.     fl_drw_text(FL_ALIGN_CENTER,ob->x,ob->y,ob->w,ob->h,ob->col2,
  42.                         sp->fontsize,sp->fontstyle,sp->items[sp->val]);
  43. }
  44.  
  45. static int mousebutton=0;
  46.  
  47. static int handle_choice(FL_OBJECT *ob,int event,float mx,float my,char key)
  48. /* Handles an event, returns whether value has changed. */
  49. {
  50.   int val,i;
  51.   long menu;
  52.   SPEC *sp = ((SPEC *)(ob->spec));
  53.   switch (event)
  54.   {
  55.     case FL_DRAW:
  56.         /* Draw the object */
  57.         draw_choice(ob);
  58.         return 0;
  59.     case FL_PUSH:
  60.     mousebutton = key;
  61.         if (sp->numitems == 0) return 0;
  62.         sp->counter = 0;
  63.         if (mousebutton == 1)
  64.         {
  65.           menu = newpup();
  66.           for (i=1; i<=sp->numitems;i++) addtopup(menu,sp->items[i]);
  67.           val =  (int) dopup(menu);
  68.       qenter(RIGHTMOUSE,0); /* Eaten up by the menu call. */
  69.           if(val > 0) sp->val = val;
  70.           freepup(menu);
  71.           fl_redraw_object(ob);
  72.         }
  73.         return 0;
  74.     case FL_MOUSE:
  75.         if (sp->numitems == 0) return 0;
  76.         if (mousebutton == 3 && sp->counter++ % 20 == 0)
  77.         {
  78.           if(sp->val == sp->numitems) sp->val = 1; else sp->val++;
  79.           fl_redraw_object(ob);
  80.     }
  81.         else if (mousebutton == 2 && sp->counter++ % 20 == 0)
  82.         {
  83.           if(sp->val <= 1) sp->val = sp->numitems; else sp->val--;
  84.           fl_redraw_object(ob);
  85.     }
  86.         return 0;
  87.     case FL_RELEASE:
  88.     mousebutton = 0;
  89.         if (sp->numitems == 0) return 0;
  90.         return 1;
  91.     case FL_ENTER:
  92.     case FL_LEAVE:
  93.         if (ob->col1 == FL_CHOICE_COL1) fl_redraw_object(ob);
  94.         return 0;
  95.     case FL_FREEMEM:
  96.     for (i=1; i<=sp->numitems;i++) free(sp->items[i]);
  97.     free(ob->spec);
  98.     return 0;
  99.  
  100.     default:
  101.         return 0;
  102.   }
  103. }
  104.  
  105. /*-------------------------------------------*/
  106.  
  107. FL_OBJECT *fl_create_choice(int type,float x,float y,float w,float h,
  108. const char *label)
  109. /* creates an object */
  110. {
  111.   FL_OBJECT *ob;
  112.   int i;
  113.   SPEC *sp;
  114.   ob = fl_make_object(FL_CHOICE,type,x,y,w,h,label,handle_choice);
  115.   ob->boxtype = FL_CHOICE_BOXTYPE;
  116.   ob->col1 = FL_CHOICE_COL1;
  117.   ob->col2 = FL_CHOICE_COL2;
  118.   ob->lcol = FL_CHOICE_LCOL;
  119.   ob->align = FL_CHOICE_ALIGN;
  120.   ob->spec = (int *) fl_malloc(sizeof(SPEC));
  121.   sp = ((SPEC *)(ob->spec));
  122.   sp->val = 0;
  123.   sp->numitems = 0;
  124.   sp->fontsize = FL_NORMAL_FONT;
  125.   sp->fontstyle = FL_NORMAL_STYLE;
  126.   for(i = 0; i <= FL_CHOICE_MAXITEMS; i++) sp->items[i] = NULL;
  127.   return ob;
  128. }
  129.  
  130. FL_OBJECT *fl_add_choice(int type, float x, float y, float w, float h, const
  131. char *label)
  132. /* Adds an object */
  133. {
  134.   FL_OBJECT *ob;
  135.   ob = fl_create_choice(type,x,y,w,h,label);
  136.   fl_add_object(fl_current_form,ob);
  137.   return ob;
  138. }
  139.  
  140. /*-------------------------------------*/
  141.  
  142. void fl_clear_choice(FL_OBJECT *ob)
  143. /* Clears the choice object */
  144. {
  145.   SPEC *sp = ((SPEC *)(ob->spec));
  146.   sp->val = 0;
  147.   sp->numitems = 0;
  148.   fl_redraw_object(ob);
  149. }
  150.  
  151. void fl_addto_choice(FL_OBJECT *ob, const char *str)
  152. /* Adds a line to the choice item. */
  153. {
  154.   SPEC *sp = ((SPEC *)(ob->spec));
  155.   if (sp->numitems >= FL_CHOICE_MAXITEMS) return;
  156.   sp->numitems++;
  157.   if (sp->items[sp->numitems] == NULL)
  158.     sp->items[sp->numitems] = (char *) fl_malloc(FL_CHOICE_MAXSTR+1);
  159.   strncpy(sp->items[sp->numitems],str,FL_CHOICE_MAXSTR);
  160.   sp->items[sp->numitems][FL_CHOICE_MAXSTR] = NULL;
  161.   if (sp->val == 0) { sp->val = 1; fl_redraw_object(ob); }
  162. }
  163.  
  164. void fl_replace_choice(FL_OBJECT *ob, int numb, const char *str)
  165. /* Replaces a line to the choice item. */
  166. {
  167.   SPEC *sp = ((SPEC *)(ob->spec));
  168.   if (numb<1 || numb >sp->numitems) return;
  169.   strncpy(sp->items[numb],str,FL_CHOICE_MAXSTR);
  170.   sp->items[numb][FL_CHOICE_MAXSTR] = NULL;
  171.   if (sp->val == numb) fl_redraw_object(ob);
  172. }
  173.  
  174. void fl_delete_choice(FL_OBJECT *ob, int numb)
  175. /* Removes a line from the choice item. */
  176. {
  177.   int i;
  178.   SPEC *sp = ((SPEC *)(ob->spec));
  179.   if (numb<1 || numb >sp->numitems) return;
  180.   free(sp->items[numb]);
  181.   for (i=numb; i<sp->numitems;i++) sp->items[i] = sp->items[i+1];
  182.   sp->items[sp->numitems] = NULL;
  183.   sp->numitems--;
  184.   if (sp->val == numb)
  185.   {
  186.     if (sp->val > sp->numitems) sp->val = sp->numitems;
  187.     fl_redraw_object(ob);
  188.   }
  189.   else if (sp->val > numb) sp->val--;
  190. }  
  191.  
  192. /*------------------------------------------*/
  193.  
  194. void fl_set_choice(FL_OBJECT *ob, int choice)
  195. /* Sets the number of the choice. */
  196. {
  197.   SPEC *sp = ((SPEC *)(ob->spec));
  198.   if (choice<1 || choice >sp->numitems) sp->val = 0; else sp->val = choice;
  199.   fl_redraw_object(ob);
  200. }
  201.  
  202. int fl_get_choice(FL_OBJECT *ob)
  203. /* Returns the number of the choice. */
  204. {
  205.   SPEC *sp = ((SPEC *)(ob->spec));
  206.   return sp->val;
  207. }
  208.  
  209. char *fl_get_choice_text(FL_OBJECT *ob)
  210. /* Returns the text of the choice. */
  211. {
  212.   SPEC *sp = ((SPEC *)(ob->spec));
  213.   if (sp->val == 0) return NULL;
  214.   return sp->items[sp->val];
  215. }
  216.  
  217. /*--------------------------------------------------*/
  218.  
  219. void fl_set_choice_fontsize(FL_OBJECT *ob, float size)
  220. /* Sets the font size inside the choice. */
  221. {
  222.   ((SPEC *)(ob->spec))->fontsize = size;
  223.   fl_redraw_object(ob);
  224. }
  225.  
  226. void fl_set_choice_fontstyle(FL_OBJECT *ob, int style)
  227. /* Sets the font style inside the choice. */
  228. {
  229.   ((SPEC *)(ob->spec))->fontstyle = style;
  230.   fl_redraw_object(ob);
  231. }
  232.